package com.jeez.module.sys.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.jeez.common.entity.DataEntity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author: Felix
 * @date: 2018-7-1
 * @version: v1.0
 * @description:
 */
@Entity
@Table(name = "t_sys_user")
@Getter
@Setter
public class SysUser extends DataEntity<SysUser> {

    @NotNull(message="登录名不能为空")
    @Length(min=1, max=60, message="登录名长度必须介于 1 和 60 之间")
    @Basic
    @Column(name = "user_code", nullable = false, insertable = true, updatable = true, length = 60)
    private String userCode;

    @NotNull(message = "姓名不能为空")
    @Length(min=1, max=60, message="姓名长度必须介于 1 和 60 之间")
    @Basic
    @Column(name = "user_name", nullable = false, insertable = true, updatable = true, length = 60)
    private String userName;

    @Basic
    @Column(name = "nick_name", nullable = true, insertable = true, updatable = true, length = 60)
    private String nickName;  //别名

    @JsonIgnore
    @Length(min=1, max=128, message="密码长度必须介于 1 和 128 之间")
    @Basic
    @Column(name = "password", nullable = true, insertable = true, updatable = true, length = 128)
    private String password;

    @NotNull(message = "工号不能为空")
    @Length(min=1, max=20, message="工号长度必须介于 1 和 20 之间")
    @Basic
    @Column(name = "staff_no", nullable = true, insertable = true, updatable = true, length = 20)
    private String staffNo;

    @Basic
    @Column(name = "dept_id", nullable = true, insertable = true, updatable = true)
    private Long deptId;  //部门

    @Length(min=0, max=15, message="电话长度必须介于 1 和 15 之间")
    @Basic
    @Column(name = "mobile", nullable = true, insertable = true, updatable = true, length = 15)
    private String mobile;

    @Length(min=0, max=15, message="手机长度必须介于 1 和 15 之间")
    @Basic
    @Column(name = "phone", nullable = true, insertable = true, updatable = true, length = 16)
    private String phone;

    @Email(message="邮箱格式不正确")
    @Length(min=0, max=100, message="邮箱长度必须介于 1 和 100 之间")
    @Basic
    @Column(name = "email", nullable = true, insertable = true, updatable = true, length = 100)
    private String email;

    @Basic
    @Column(name = "status", nullable = false, insertable = true, updatable = true, length = 4)
    private String status;  //状态

    @Basic
    @Column(name = "login_flag", nullable = true, insertable = true, updatable = true, length = 1)
    private String loginFlag; // 是否允许登陆

    @Basic
    @Column(name = "remark", nullable = true, insertable = true, updatable = true, length = 400)
    private String remark; //备注


    /**
     * @JoinTable描述了多对多关系的数据表关系。name属性指定中间表名称,joinColumns定义中间表与User表的外键关系
     *
     */
    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "t_sys_user_role",
               joinColumns = {@JoinColumn(name = "user_id")},
               inverseJoinColumns = {@JoinColumn(name = "role_id")} )
    @JsonIgnore
    private List<SysRole> roles = Lists.newArrayList();

    @OneToOne(targetEntity = SysUserExt.class, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    @JoinColumn(name = "id", referencedColumnName = "id")
    private SysUserExt userExt;
}
